package client.gui.roombox;

import stalker.Stalker;
import client.network.ServiceRequestCenter;
import client.network.interactive.InviteUserByNameRequest;
import data.content.Content;

public class RoomBoxController implements RoomController {
  
  private final Stalker room_;
  private final ServiceRequestCenter servicer_;
  private final RoomBoxView view_;
  
  public Stalker getRoom() {
    return room_;
  }
  
  public RoomBoxController(Stalker room, ServiceRequestCenter servicer, RoomBoxModel model) {
    room_ = room;
    servicer_ = servicer;
    view_ = new RoomBoxView(room.getName(), this);
    model.addListener(view_);
  }
  
  public void requestInviteUser(String username) {
    servicer_.serviceRequest(
        new InviteUserByNameRequest(room_, username));    
  }

  public void requestWriteContent(Content content) {
    servicer_.requestWriteToRoom(room_, content);
  }
  
  public void requestJoinRoom() {
    servicer_.requestJoinRoom(room_);
  }
  
  public void requestLeaveRoom() {
    servicer_.requestLeaveRoom(room_);
  }
  
  public boolean isVisible() {
    return view_.isVisible();
  }
  
  public void setVisible(boolean status) {
    if (!(view_.isVisible()) && status) {
      // this will make window visible
      requestJoinRoom();
    }
    if ((view_.isVisible()) && (!status)) {
      // this will close the window
      requestLeaveRoom();
    }
    view_.setVisible(status);
  }

}
